<?php

namespace globalunit\utils;

use Swoole\Coroutine;

use QYS\Log\Log;

function pack($s){
    $len = strlen($s);
    $a = floor($len/256);
    $b = floor($len%256);
    $head = chr($a).chr($b);
    $arr = [$head, $s];
    return implode("", $arr);
}

class SocketClient
{

    public static function send($data)
    {
        $socket = new Coroutine\Socket(AF_INET, SOCK_STREAM, 0);
        $retval = $socket->connect('127.0.0.1', 9601);
        if ($retval) {
            $socket->send(pack($data));
            //发生错误或对端关闭连接，本端也需要关闭
            $socket->close();
        }
    }
}
